/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qb9.flashlib.profiling
{
	import com.qb9.flashlib.interfaces.IDisposable;
	import com.qb9.flashlib.interfaces.IMoveable;
	
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * This textfield simplifies positioning and contains a default styling.
	 * It's useful for small signs that show things like fps or ram consumption.
	 */

	public class Meter extends TextField implements IDisposable, IMoveable
	{
		protected var _x:Number;
		protected var _y:Number;
		
		public function Meter(x:Number, y:Number)
		{
			_x = x;
			_y = y;
			
			init();
		}
		
		private function init():void
		{
			style();
			addEventListener(Event.ADDED_TO_STAGE, position);
		}
		
		protected function style():void
		{
			defaultTextFormat = new TextFormat('Arial', 9);
			selectable = false;
			textColor = 0x00FF00;
		}
		
		protected function position(e:Event):void
		{
			if( _x < 0 )
			{
				autoSize = TextFieldAutoSize.RIGHT;
				x = _x + stage.stageWidth;
			}
			else
			{
				autoSize = TextFieldAutoSize.LEFT;
				x = _x;	
			}
			
			if( _y < 0 )
				y = _y + stage.stageHeight - textHeight;
			else
				y = _y;
		}
		
		public function dispose():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, position);
		}
	}
}
